import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

public class ServerCapture {
	final static String IMG_FORMAT_JPG = "jpg";
	Rectangle rect;
	public Rectangle getRect() {
		return rect;
	}
	public void setRect(Rectangle rect) {
		this.rect = rect;
	}
	public boolean takePicture(String path, String fileName, String format) {
		String fullName = path;
		if(!path.endsWith("\\"))
			fullName += "\\";
		fullName += fileName +"."+ format;
		try {
			BufferedImage buffImg
				= new Robot().createScreenCapture(getRect());
			File imgFile = new File(fullName);
			ImageIO.write(buffImg, format, imgFile);
		} catch (HeadlessException he) {
			he.printStackTrace();return false;
		} catch (AWTException awte) {
			awte.printStackTrace();return false;
		} catch (IOException ioe) {
			ioe.printStackTrace();return false;
		}
	return true;			
	}
	
	public static void capture(String path, String fileName, String format) {
		ServerCapture serverCapture = new ServerCapture();
		serverCapture.setRect(
				new Rectangle(
						Toolkit.getDefaultToolkit().getScreenSize()));
		if(serverCapture.takePicture(path, fileName, format))
			System.out.println("print Ok");
		serverCapture = null;
	}
}
